import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'cupertino_chinese_localizations_delegate.dart';

///flutter_boost需要的容器
///* 为了灵活将默认容器Container改为自定义StatefulWidget，通过state生命周期做一些其他操作
class ContainerApp extends StatefulWidget {
  final AppStateLifeCycle appStateLifeCycle;

  ContainerApp(this.appStateLifeCycle);

  @override
  _ContainerAppState createState() => _ContainerAppState(appStateLifeCycle);
}

class _ContainerAppState extends State<ContainerApp> {
  AppStateLifeCycle appStateLifeCycle;

  _ContainerAppState(AppStateLifeCycle appStateLifeCycle) {
    this.appStateLifeCycle = appStateLifeCycle ?? AppStateLifeCycle();
  }

  @override
  void initState() {
    appStateLifeCycle?.onAppInitState();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    appStateLifeCycle?.onAppBuild(context);
    MaterialAppParam param = Env?.value?.materialAppParam ??
        MaterialAppParam(
            themeData: ThemeData(
              primaryColor: Colors.white,
              backgroundColor: Colors.white,
              accentColor: Color(0xFF3DBCC6),
              primaryColorBrightness: Brightness.light,
              textTheme: TextTheme(
                //设置Material的默认字体样式
                title: TextStyle(
                  fontWeight: FontWeight.w500,
                ),
              ),
              primaryTextTheme: TextTheme(
                //设置Material的默认字体样式
                title: TextStyle(
                  color: Color(0xff333333),
                  fontSize: 17.0,
                  fontWeight: FontWeight.w500,
                ),
              ),
              accentTextTheme: TextTheme(
                //设置Material的默认字体样式
                title: TextStyle(
                  fontWeight: FontWeight.w500,
                ),
              ),
              primaryIconTheme: IconThemeData(
                color: Color(0xff333333),
                size: 20.0,
              ),
              appBarTheme: AppBarTheme(
                textTheme: TextTheme(
                  title: TextStyle(
                    color: Color(0xff333333),
                    fontSize: 17,
                    fontWeight: FontWeight.w500,
                  ),
                  body1: TextStyle(
                    color: Color(0xff333333),
                    fontSize: 17,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                elevation: 0.5,
                actionsIconTheme: IconThemeData(size: 16),
              ),
            ),
            builder: FlutterBoost.init());
    return ExcludeSemantics(
      excluding: true,
      child: OKToast(
        child: MaterialApp(
          theme: param.themeData,
          builder: param.builder,
          home: Scaffold(
              body: appStateLifeCycle.appWidgetBuilder(context) ?? Container()),
          localizationsDelegates: [
            CupertinoChineseLocalizationsDelegate(),
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
          ],
          supportedLocales: Platform.isIOS ? [
            const Locale('en', 'US'),
            const Locale('zh', 'CH')
          ]: [
            const Locale('zh', 'CH'),
            const Locale('en', 'US')
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    appStateLifeCycle?.onAppDispose();
    appStateLifeCycle = null;
    super.dispose();
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    appStateLifeCycle?.debugFillProperties(properties);
    super.debugFillProperties(properties);
  }

  @override
  void didChangeDependencies() {
    appStateLifeCycle?.didChangeDependencies();
    super.didChangeDependencies();
  }

  @override
  void deactivate() {
    appStateLifeCycle?.deactivate();
    super.deactivate();
  }

  @override
  void setState(VoidCallback fn) {
    appStateLifeCycle?.setState(fn);
    super.setState(fn);
  }

  @override
  void reassemble() {
    appStateLifeCycle?.reassemble();
    super.reassemble();
  }

  @override
  void didUpdateWidget(ContainerApp oldWidget) {
    appStateLifeCycle?.didUpdateWidget(oldWidget);
    super.didUpdateWidget(oldWidget);
  }
}

class AppStateLifeCycle {
  void onAppInitState() {
    Map<String, PageBuilder> result;
    Map<String, Type> map = Env?.value?.routeIml?.getRoutMapByScheme(
        Env?.value?.schemeNativeFilterName ?? "dkflutter://");
    print(map.toString());
    if (null != map) {
      map.forEach((String route, Type type) {
        if (null == result) {
          result = Map();
        }
        result[route] = (String pageName, Map params, String uniqueId) {
          return Env?.value?.routeIml?.instanceFromClazz(
              type, BoostRouteOption(pageName, params, uniqueId));
        };
      });
    }
    FlutterBoost.singleton.registerPageBuilders(result);
    FlutterBoost.handleOnStartPage();
  }

  void onAppBuild(BuildContext context) {}

  Widget appWidgetBuilder(BuildContext context) {
    return Container();
  }

  void onAppDispose() {
    Env?.value?.application?.onTerminate();
  }

  void didUpdateWidget(ContainerApp oldWidget) {}

  void reassemble() {}

  void setState(VoidCallback fn) {}

  void deactivate() {}

  void didChangeDependencies() {}

  void debugFillProperties(DiagnosticPropertiesBuilder properties) {}
}
